
import os
import requests
from fuzzywuzzy import fuzz
import pandas as pd
import __init__
import other
import dxw
import start
from get_data import Altas_db
import numpy as np
"""
1.增加板块信息
2.增加人气
3.增加换手率"""

start_time, today_time, _hour_= other.time_start()  # start_time,today_time,today_hour=时间('20200731', '20200820', '16:12')
def time_front30_data():
    day_time=other.time_saveandget(choose="day_list",day=40)
    if len(day_time)>=30:
        print(len(day_time))
    if len(day_time)<30:
        day_time = other.time_saveandget(choose="day_list", day=60)
        if len(day_time)>=30:
            pass
        if len(day_time)<30:
            day_time = other.time_saveandget(choose="day_list", day=90)
    day_time=sorted(day_time,reverse=True)#降序排列
    day_time_front30=day_time[:30]
    print(day_time_front30,len(day_time_front30))
    day_time_start=day_time_front30[-1]
    return day_time_start
#________________________________
tvp_df0,tvp_df1=Altas_db._read_ths_volMax(dtype="pri",model="TwoMaxdf")
tvp_df0.rename(columns={'5日': "p_5day", "20日": "p_20day", "50日": "p_50day", "250日": "p_250day"}, inplace=True)
tvp_df1.rename(columns={'5日': "p_5day", "20日": "p_20day", "50日": "p_50day", "250日": "p_250day"}, inplace=True)
pri_merge=pd.merge(tvp_df0[["p_5day","p_20day","p_50day","p_250day","code"]], tvp_df1[["p_5day","p_20day","p_50day","p_250day","code"]], how='inner', on='code')


tvv_df0,tvv_df1=Altas_db._read_ths_volMax(dtype="vol",model="TwoMaxdf")
tvv_df0.rename(columns={'5日': "v_5day", "30日": "v_30day", "120日": "v_120day"}, inplace=True)
tvv_df0["v_5day"]=tvv_df0["v_5day"]*10000
tvv_df0["v_30day"]=tvv_df0["v_30day"]*10000
tvv_df0["v_120day"]=tvv_df0["v_120day"]*10000
tvv_df1.rename(columns={'5日': "v_5day", "30日": "v_30day", "120日": "v_120day"}, inplace=True)
tvv_df1["v_5day"]=tvv_df1["v_5day"]*10000
tvv_df1["v_30day"]=tvv_df1["v_30day"]*10000
tvv_df1["v_120day"]=tvv_df1["v_120day"]*10000
vol_merge=pd.merge(tvv_df0[["v_5day","v_30day","v_120day","code"]], tvv_df1[["v_5day","v_30day","v_120day","code"]], how='inner', on='code')

pp=pd.merge(pri_merge, vol_merge, how='inner', on='code')
import ts_his
ts_his_model=ts_his.DmiWith_V_ma5()
con=pd.merge(ts_his_model,pp, how='inner', on='code')


def funcation(df):
    print(df.head(), len(df))
    Altas_db._save_mongo_db(df, "dxw_bk_ans", "pp11".format(today_time))
    pass
def at_10(num=10):
    df = Altas_db._readdf("ts", "2.his数据下载_fast1")
    print(len(df))
    n = list(set(df.trade_date))
    # 用sort将列表进行排序，reverse=True为降序设置
    n.sort(reverse=True)
    print(n,len(n))
    if len(n) >= 11:
        temp = df[df.trade_date == n[0]][['ts_code', "high", "low", "close", "vol"]]
        for i in range(1, len(n)):
            print(i)
            _df = df[df.trade_date == n[i]][['ts_code', "high", "low", "close", "vol"]]
            temp = pd.merge(temp, _df, how='inner', on='ts_code')
        temp.columns = [str(i) for i in range(4*len(n)+1)]
    print(temp.head())
    if len(n) < 10:
        print("请检查df,df出错误,日期少于10")
    a = np.array(temp)#temp[temp["0"]=="000001.SZ"]
    #TR = MAX(MAX(HIGH - LOW, ABS(HIGH - REF(CLOSE, 1))), ABS(LOW - REF(CLOSE, 1)))
    list_sum=[]
    for i in range(0,num):
        print(i,4*i+1,4*i+2,4*i+7)
        a1 = (a[:, 4*i+1] - a[:, 4*i+2])
        a2 = abs(a[:, 4*i+1] - a[:, 4*i+7])
        a3 = np.max([a1, a2], axis=0)
        a4 = abs(a[:, 4*i+2] - a[:, 4*i+7])
        a5= np.max([a3, a4], axis=0)
        list_sum.append(a5)
        print(i,4*i+1,4*i+2,4*i+7,a5)
    data=pd.DataFrame(list_sum).T
    temp["atr_today10"]=data[[0,1,2,3,4,5,6,7,8,9]].mean(axis=1)
    # temp["Mtr"]=list_sum
    print(temp[temp.atr_today10/temp["3"]>0.06])
def at_cross19day(num=10):
    df = Altas_db._readdf("ts", "2.his数据下载_fast1")
    print(len(df))
    n = list(set(df.trade_date))
    # 用sort将列表进行排序，reverse=True为降序设置
    n.sort(reverse=True)
    print(n,len(n))
    if len(n) >= 11:
        temp = df[df.trade_date == n[0]][['ts_code', "high", "low", "close", "vol"]]
        for i in range(1, len(n)):
            print(i)
            _df = df[df.trade_date == n[i]][['ts_code', "high", "low", "close", "vol"]]
            temp = pd.merge(temp, _df, how='inner', on='ts_code')
        temp.columns = [str(i) for i in range(4*len(n)+1)]
    print(temp.head())
    if len(n) < 10:
        print("请检查df,df出错误,日期少于10")
    a = np.array(temp)#temp[temp["0"]=="000001.SZ"]
    #TR = MAX(MAX(HIGH - LOW, ABS(HIGH - REF(CLOSE, 1))), ABS(LOW - REF(CLOSE, 1)))
    list_sum=[]
    for i in range(0,num):
        print(i,4*i+1,4*i+2,4*i+7)
        a1 = (a[:, 4*i+1] - a[:, 4*i+2])
        a2 = abs(a[:, 4*i+1] - a[:, 4*i+7])
        a3 = np.max([a1, a2], axis=0)
        a4 = abs(a[:, 4*i+2] - a[:, 4*i+7])
        a5= np.max([a3, a4], axis=0)
        list_sum.append(a5)
        print(i,4*i+1,4*i+2,4*i+7,a5)
    data=pd.DataFrame(list_sum).T
    temp["atr_today10"]=data[[0,1,2,3,4,5,6,7,8,9]].mean(axis=1)
    # temp["Mtr"]=list_sum
    print(temp[temp.atr_today10/temp["3"]>0.06])
if __name__ == '__main__':
    #funcation(con)
    at_10()